from models.permission import Permission
from schemas.users import PermissionsResponse

class PermissionRepository:
    def __init__(self, db):
        self.db = db

    def create_or_update_permission(self, permission: Permission) -> Permission:
        self.db.add(permission)
        self.db.commit()
        self.db.refresh(permission)
        return permission

    def get_permission_obj_by_id(self, permission_id:int) -> Permission:
        return self.db.query(Permission).filter_by(id=permission_id).first()


    def get_permission_by_name(self, name:str) -> Permission:
        return self.db.query(Permission).filter_by(name=name).first()

    def get_permissions_by_father_id(self, father_id:int) -> list[Permission]:
        return self.db.query(Permission).filter_by(father_id=father_id).all()

    def filter_permissions(self, page:int|None, size:int|None) -> list[Permission]:
        return self.db.query(Permission).offset((page-1)*size).limit(size).all()

    def get_permissions_count(self) -> int:
        return self.db.query(Permission).count()

    def delete_permission(self, permission_id:int):
        permission = self.db.query(Permission).filter_by(id=permission_id).first()
        self.db.delete(permission)
        self.db.commit()
        return
